//
//  NetworkManager.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/23.
//

import Foundation
import Moya

class AiNetManager {
    static let shared = AiNetManager()
    
    private let provider: MoyaProvider<AiNetApi>
    
    private init() {
        provider = MoyaProvider<AiNetApi>()
    }
    //统一请求处理
    private func request<M: Codable>(_ target: AiNetApi, modeType: M.Type) async throws-> M {
        try await withCheckedThrowingContinuation({ continuation in
            provider.request(target) { result in
                //调用统一接口处理
                dealNetResult(result: result, modeType: modeType) {
                    continuation.resume(returning: $0)
                } fail: {
                    continuation.resume(throwing: $0)
                }
            }
        })
    }
}

//接口请求
extension AiNetManager {
    
    /**
     *  请求推送
     */
    func requestPush<M: Codable>(jsonString : String, modeType: M.Type) async throws-> M {
        let api : AiNetApi = .push(jsonString: jsonString)
        return try await request(api, modeType: modeType)
    }
    /**
     *  删除推送
     */
    func deletePush<M: Codable>(jsonString : String, modeType: M.Type) async throws-> M {
        let api : AiNetApi = .deletePush(jsonString: jsonString)
        return try await request(api, modeType: modeType)
    }
    
    /**
     *  ai请求 minmax
     */
    func requestAIMinMaxVoice<M: Codable>(jsonObject: String?, modeType: M.Type) async throws-> M {
        let token = configManager.getConfig().aiChat.token ?? ""
        let groupId = configManager.getConfig().aiChat.groupId ?? ""
        let api : AiNetApi = .aiMinmaxVoice(token: token, group_id: groupId, jsonString: jsonObject ?? "")
        return try await request(api, modeType: modeType)
    }
    
    /**
     *  ai请求 minmax
     */
    func requestAIMinMax<M: Codable>(jsonObject: String?, modeType: M.Type) async throws-> M {
        let token = configManager.getConfig().aiChat.token ?? ""
        let groupId = configManager.getConfig().aiChat.groupId ?? ""
        let api : AiNetApi = .aiMinmax(token: token, group_id: groupId, jsonString: jsonObject ?? "")
        return try await request(api, modeType: modeType)
    }
    
    /**
     *  ai请求（豆包或者rtsagi 通用）
     */
    func requestAI<M: Codable>(host : String?, token : String?, jsonObject: String?, modeType: M.Type) async throws-> M {
        if((host?.isUrl() ?? false) != true){
            throw MyError.netFail(400, "地址填写错误")
        }
        let api : AiNetApi = .aiCommon2(url: host ?? "", token: token ?? "", jsonString: jsonObject ?? "")
        return try await request(api, modeType: modeType)
    }
    
    /**
     *  ai请求（豆包或者rtsagi 通用）
     */
    func requestAI<M: Codable>(host : String?, token : String?, jsonObject: Encodable, modeType: M.Type) async throws-> M {
        if((host?.isUrl() ?? false) != true){
            throw MyError.netFail(400, "地址填写错误")
        }
        let api : AiNetApi = .aiCommon(url: host ?? "", token: token ?? "", jsonString: jsonObject)
        return try await request(api, modeType: modeType)
    }
    
}
